package springBoot.akai.Akai.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springBoot.akai.Akai.Entity.Video;
import springBoot.akai.Akai.Mapper.TagMapper;
import springBoot.akai.Akai.Mapper.VideoMapper;

@Controller
public class VideoController {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TagMapper tagMapper;

    @RequestMapping("/video/add")
    String videoAddView() {
        return "Video/add";
    }
    
    @RequestMapping("/video/edit")
    String videoEditView(Model model, int id) {
    	model.addAttribute("video", videoMapper.selectById(id));
    	
        return "Video/edit";
    }
    
    @RequestMapping("/video/detail")
    String videoDetailView(Model model, int id) {
    	model.addAttribute("video", videoMapper.selectById(id));
    	model.addAttribute("tags", tagMapper.selectByVideoId(id));
    	
        return "Video/detail";
    }
    
    @RequestMapping("/video/tagList")
    String videoTagListView(Model model, int tagId) {
    	model.addAttribute("tag", tagMapper.selectById(tagId));
    	model.addAttribute("list", tagMapper.selectVideoByTagId(tagId));
    	
    	return "Video/list";
    }

    @RequestMapping(value = "/video/add", method = RequestMethod.POST)
    String handleVideoAdd(Model model, Video video) {
        model.addAttribute("url", "/");
        model.addAttribute("text", "返回主页");

        if (videoMapper.insert(video)) {
            model.addAttribute("info", "添加视频成功！");

            return "Result/success";
        } else {
            model.addAttribute("info", "添加视频失败！");

            return "Result/failed";
        }
    }
    
    @RequestMapping(value = "/video/edit", method = RequestMethod.POST)
    String handleVideoEdit(Model model, Video video) {
        model.addAttribute("url", "/");
        model.addAttribute("text", "返回主页");

        if (videoMapper.update(video)) {
            model.addAttribute("info", "修改视频信息成功！");

            return "Result/success";
        } else {
            model.addAttribute("info", "修改视频信息失败！");

            return "Result/failed";
        }
    }

    @RequestMapping("/video/remove")
    String handleVideoRemove(Model model, int id) {
        model.addAttribute("url", "/");
        model.addAttribute("text", "返回主页");

        if (videoMapper.deleteById(id)) {
            model.addAttribute("info", "删除视频成功！");

            return "Result/success";
        } else {
            model.addAttribute("info", "删除视频失败！");

            return "Result/failed";
        }
    }
}
